package cn.hx.controller.front.user;

import cn.hx.entity.UserInfo;
import cn.hx.service.UserInfoService;
import com.sun.media.sound.SoftTuning;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class ForgetController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private JavaMailSenderImpl mailSender;

    @GetMapping("signOut")
    public String signout(HttpServletRequest Request){
        Request.getSession().removeAttribute("userInfo");
        return "redirect:/index";
    }

    @PostMapping("user/sendEmail")
    @ResponseBody
    public Map checkEmail(String email, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        UserInfo userInfo = userInfoService.queryByEmail(email);
        if (userInfo != null) {
            String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 5);
            System.out.println(uuid);
            session.setAttribute("checkCode", uuid);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("1533481985@qq.com");
            message.setTo(email);
            message.setSubject("修改密码所需要的验证码");
            message.setText(uuid);
            mailSender.send(message);
            System.out.println("邮件发送完毕");
            Integer id = userInfo.getId();
            session.setAttribute("id", id);
            map.put("code", 1);
        } else {
            map.put("code", 0);
            map.put("msg", "邮箱不存在");
        }
        return map;
    }

    @PostMapping("user/editPassword")
    @ResponseBody
    public Map editPs(String password,
                      String checkCode,
                      HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Object checkCode1 = session.getAttribute("checkCode");
        Integer id = (Integer) session.getAttribute("id");
        if (checkCode1.equals(checkCode)) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setPassword(password);
            int update = userInfoService.update(userInfo);
            if (update == 1) {
                session.removeAttribute("checkCode");
                session.removeAttribute("id");
                map.put("code", 1);
            } else {
                map.put("code", 0);
                map.put("msg", "修改失败");
            }
        } else {
            map.put("code", 0);
            map.put("msg", "验证码错误");
        }
        return map;
    }

    /*登录注册*/
    @ResponseBody
    @PostMapping("user/regist")
    public Map regist(UserInfo userInfo) {
        HashMap<Object, Object> map = new HashMap<>();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setTelephone(userInfo.getTelephone());
        userInfo1.setEmail(userInfo.getEmail());
        userInfo1.setIdCard(userInfo.getIdCard());
        UserInfo userInfo2 = userInfoService.queryByEmail(userInfo.getEmail());
        UserInfo userInfo3 = userInfoService.queryByTelephone(userInfo.getTelephone());
        UserInfo userInfo4 = userInfoService.queryByIdCard(userInfo.getIdCard());
        if (userInfo2 != null || userInfo3 != null || userInfo4 != null) {
            map.put("flag", 0);
            if (userInfo2 != null) {
                map.put("msg", "注册失败！此邮箱已经存在！！！请前往登录界面下的忘记密码");
                return map;
            }
            if (userInfo3 != null) {
                map.put("msg", "注册失败！此电话号码已经存在！！！");
                return map;
            }
            if (userInfo4 != null) {
                map.put("msg", "注册失败！此身份ID已经存在！！！");
                return map;
            }

        }
        userInfo.setCreateTime(null);
        userInfoService.insert1(userInfo);
        map.put("msg", "注册成功！");
        return map;
    }

    @ResponseBody
    @PostMapping("index/userLogin")
    public Map login(String Telephone, String passWord, String verificationCode, HttpSession session) {
        HashMap<Object, Object> map = new HashMap<>();
        String verifyCode = (String) session.getAttribute("verifyCode");
        if (verifyCode.equalsIgnoreCase(verificationCode)) {
            UserInfo userInfo1 = userInfoService.queryByTelephone(Telephone);
            if (userInfo1 != null) {
                if (passWord.trim().equals(userInfo1.getPassword().trim())) {
                    session.setAttribute("userInfo", userInfo1);
                    /*if(userInfo1.getRole().getId()==1){
                        map.put("result",2);
                        map.put("msg", "登录成功！");
                        return map;
                    }*/
                    map.put("result",1);
                    map.put("msg", "登录成功！");
                    return map;
                }else {
                    map.put("result",0);
                    map.put("msg", "登录密码错误！");
                    return map;
                }
            }
            map.put("msg", "用户不存在！");
            return map;
        }
        map.put("result",0);
        map.put("msg", "验证码输入错误！");
        return map;
    }
}
